import { Injectable } from '@angular/core';
import {MsgResult} from '../../models/msg-result';
import {API} from '../../constants/constants';
import {Observable} from 'rxjs/Observable';
import {RequestUtil} from '../../util/request-util';
import {FormUtil} from '../../util/form-util';
import {Rescatalog} from './rescatalog';

@Injectable()
export class RescatalogService {

  constructor(
    private requestUtil: RequestUtil,
    private formUtil: FormUtil
  ) { }

  /**
   * 获取资源目录树
   * @returns {Observable<MsgResult>}
   */
  getRescatalog(): Observable<MsgResult> {
    const params = 'isUse=1';
    return this.requestUtil.get(API.getRescatalog, params);
  }

  /**
   * 保存新增资源目录
   * @param resCatalog
   * @returns {Observable<MsgResult>}
   */
  postRescatalog(resCatalog: Rescatalog): Observable<MsgResult> {
    const params = this.formUtil.serialize(resCatalog);
    return this.requestUtil.post(API.postRescatalog, params);
  }


  /**
   * 修改资源目录
   * @param resCatalog
   * @returns {Observable<MsgResult>}
   */
  updateRescatalog(resCatalog: Rescatalog): Observable<MsgResult> {
    const params = this.formUtil.serialize(resCatalog);
    return this.requestUtil.put(API.rescatalogById.replace('{id}', resCatalog.id), params);
  }

  /**
   * 删除资源目录
   * @param id
   * @returns {Observable<MsgResult>}
   */
  deleteRescatalog(id): Observable<MsgResult> {
    return this.requestUtil.delete(API.rescatalogById.replace('{id}', id));
  }

  /**
   * 通过id获取资源目录信息
   * @param id
   * @returns {Observable<MsgResult>}
   */
  getRescatalogById(id): Observable<MsgResult> {
    return this.requestUtil.get(API.rescatalogById.replace('{id}', id));
  }
}
